package cn.jwelll.bxcstock.ui.train

import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.EditorInfo
import cn.jwelll.bxcstock.R
import cn.jwelll.bxcstock.base.DataBindingActivity
import cn.jwelll.bxcstock.bean.RowsBean
import cn.jwelll.bxcstock.common.ConstValue
import cn.jwelll.bxcstock.common.gone
import cn.jwelll.bxcstock.common.show
import cn.jwelll.bxcstock.ui.adapter.CommonRecyclerAdapter
import cn.jwelll.bxcstock.ui.view.recycleview.HorizontalDividerDecoration
import cn.jwelll.bxcstock.ui.view.recycleview.WrapContentManager
import cn.jwelll.bxcstock.ui.view.recycleview.XRecyclerView
import cn.jwelll.bxcstock.util.DisplayUtil
import cn.jwelll.bxcstock.util.SPUtils
import cn.jwelll.bxcstock.viewmodel.itemmodel.ItemTrainModel
import kotlinx.android.synthetic.main.activity_in_train.*
import kotlinx.android.synthetic.main.layout_recycleview.*

class TrainInActivity : DataBindingActivity<ItemTrainModel, TrainContract.View, TrainPresenter>(), TrainContract.View,
    XRecyclerView.LoadingListener {
    private var mPage = 1
    private var mFilter = ""
    private var mLoadMore = false
    private val adapter by lazy {
        CommonRecyclerAdapter<ItemTrainModel>(this, R.layout.item_train_in, ArrayList()) { v, t, _ ->
        }
    }

    override fun createPresenter() = TrainPresenter(this)

    override fun onRefresh() {
        mFilter = ""
        mPage = 1
        mLoadMore = false
        mPresenter.trainGetList(mFilter, mPage)
    }

    override fun onLoadMore() {
        mPage++
        mLoadMore = true
        mPresenter.trainGetList(mFilter, mPage)
    }

    override fun trainGetListSuccess(model: RowsBean<ItemTrainModel>?) {
        if (mLoadMore) recycler_view.loadMoreComplete() else recycler_view.refreshComplete()
        hideProgress()
        model?.items?.let {
            model?.items?.forEach {
                for ((index, value) in it.materials.withIndex()!!) {
                    value.index = index + 1
                }
            }
            ll_empty.gone()
            if (mLoadMore) {
                recycler_view.loadMoreComplete()
                adapter.insertData(it)
            } else {
                recycler_view.refreshComplete()
                adapter.refresh(it)
            }
            recycler_view.setNoMore(it.size < 10)
            ll_empty?.visibility = if (adapter.itemCount == 0) View.VISIBLE else View.GONE
        } ?: let {
            if (mLoadMore) {
                recycler_view.setNoMore(true)
                recycler_view.loadMoreComplete()
                ll_empty.gone()
            } else {
                recycler_view.setNoMore(false)
                recycler_view.refreshComplete()
                adapter.clear()
                ll_empty.show()
            }
        }
    }


    override fun getContentView() = R.layout.activity_in_train

    override fun getTitleString() = SPUtils.getString(this, ConstValue.WARE_HOUSE_NAME, "") + "火车装车"

    override fun initView() {
        super.initView()
        initRecycleView()
        showProgress("")
        mPresenter.trainGetList(mFilter, mPage)
    }

    override fun initEvent() {
        super.initEvent()

        et_car_num.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                ib_delete.visibility = if (et_car_num.text.toString().trim().isEmpty()) View.GONE else View.VISIBLE
                mFilter = et_car_num.text.toString().trim()
                mPage = 1
                mLoadMore = false
                showProgress("")
                mPresenter.trainGetList(mFilter, mPage)
            }

            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
        })
        ib_delete.setOnClickListener {
            et_car_num.setText("")
        }
    }


    private fun initRecycleView() {
        val mLinearLayoutManager = WrapContentManager(this, LinearLayoutManager.VERTICAL, false)
        recycler_view.layoutManager = mLinearLayoutManager
        recycler_view.addItemDecoration(
            HorizontalDividerDecoration(
                this,
                R.drawable.divider_horizontal_10_ffffff,
                DisplayUtil.dip2px(this, 10f)
            )
        )
        recycler_view.setHasFixedSize(true)
        recycler_view.setLoadingMoreEnabled(false)
        recycler_view.setPullRefreshEnabled(true)
        recycler_view.refreshComplete()
        recycler_view.loadMoreComplete()
        recycler_view.setLoadingListener(this)

        recycler_view.adapter = adapter
    }
}